//
//  EnterpriseAPI.swift
//  CooperSwift
//
//  Created by Mac on 2019/12/24.
//  Copyright © 2019 CooperSwift. All rights reserved.
//

import Foundation
import UIKit
import Moya
import CryptoSwift
import SwiftyJSON
import CooperSwift

// 初始化Httpbin.org请求的provider

//let EnterpriseProvider = MoyaProvider<Enterprise>(plugins: [LogPlugin()])

// LogPlugin(),
let EnterpriseProvider = MoyaProvider<Enterprise>(plugins: [SpinnerPlugin(), EncryptPlugin(), LogPlugin(), RequestAlertPlugin()])

//let Provider = EnterpriseProvider()
//
//func EnterpriseProvider() -> MoyaProvider<Enterprise> {
//    if let view = UIApplication.currentVC()?.view {
//        let provider = MoyaProvider<Enterprise>(plugins: [
//            RequestAlertPlugin(view: view),
//            AuthPlugin(tokenClosure: { () -> String? in
//                return ""
//            }),
//            LogPlugin() // EncryptPlugin()
//        ])
//        return provider
//    }
//    return MoyaProvider<Enterprise>()
//}

public enum Enterprise {
    case showProvince
    case showCity(String)
    case showCulture(String, Int)
}

// 请求配置
extension Enterprise: TargetType {
    
    public var method: Moya.Method {
        return .post
    }
    
    public var sampleData: Data {
        return "{}".data(using: String.Encoding.utf8)!
    }
    
    public var task: Task {
        var params: [String:Any] = [:]
        
        switch self {
        case .showProvince:
            params["provinceId"] = "xx"
        case .showCity(let provinceId):
            params["provinceId"] = provinceId
        case .showCulture(let companyCode, let page):
            params["companyCode"] = companyCode
            params["page"] = "\(page)"
        default:
//            return .requestPlain
            break
        }
        
        params["timestamp"] = "\(Int(NSDate.init().timeIntervalSince1970 * 1000))"
        let keys = params.keys.sorted(by: <)
        var strs = ""
        for key in keys {
            if let value = params[key] {
                if let val = value as? String {
                    strs += (key + "=" + val.trimmingCharacters(in: .whitespaces).trimmingCharacters(in: .whitespacesAndNewlines) + "&")
                }else {
                    strs += (key + "=" + "\(value)" + "&")
                }
            }
        }
        strs += ("key=" + "dcxj_hd")
        params["sign"] = strs.md5().uppercased()
        return .requestParameters(parameters: params,
                                  encoding: URLEncoding.default)
    }
    
    public var headers: [String : String]? {
        return nil
    }
    
    
    public var baseURL: URL {
        return URL(string: "http://101.200.47.68:8080/home/interface/")!
    }
    
    public var path: String {
        switch self {
        case .showProvince:
            return "address/showProvince"
        case .showCity:
            return "address/showCity"
        case .showCulture:
            return "library/showCulture"
        }
    }
    
}
